package com.yld.car.market;

import java.util.ArrayList;
import java.util.List;

import cn.yld.car.db.DialogCityDB;

import com.yld.car.common.NetworkProgressUtils;
import com.yld.car.domain.DialogCityItem;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class PersonCityActivity extends BaseActivity implements
		OnItemClickListener {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		setContentView(R.layout.base_list);
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title);

		this.settingTitle(R.id.title, "选择城市");

		list = (ListView) findViewById(R.id.list1);
		list.setOnItemClickListener(this);
		// Intent intent = this.getIntent();
		//
		// cId = intent.getStringExtra("cId");
		// System.out.println("cId=====" + cId);
		utils = NetworkProgressUtils.getInstance();

		Intent intent = this.getIntent();
		name = intent.getStringExtra("name");
		initViews(name);
	}

	private NetworkProgressUtils utils;
	private ListView list;
	private String name;

	private void initViews(String name) {

		// DialogCityDB dB=new DialogCityDB();
		List<DialogCityItem> all = DialogCityDB.getCityInfo(this, name);
		List<String> names = new ArrayList<String>();
		for (DialogCityItem d : all) {
			names.add(d.getName());
		}

		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
				R.layout.base_list_items_simple, R.id.itemContent, names) {

			@Override
			public View getView(int position, View convertView, ViewGroup parent) {

				convertView = super.getView(position, convertView, parent);
				convertView
						.setBackgroundResource(android.R.drawable.list_selector_background);
				return convertView;
			}
		};
		list.setAdapter(adapter);

	}

	@Override
	public void onItemClick(AdapterView<?> adapterView, View view,
			int position, long id) {

		String cityName = adapterView.getAdapter().getItem(position).toString();

		// mApp.setmProvinceInfo(name + "-" + cityName);

		mApp.setmCityInfo(cityName);
		finish();

	}

}
